import re
import queue_globals as vars
from queue_handler import queue_handler


# This DUDE DOES NOT run in his own thread. We assume that all
# requests will come to him via request_handler.
class handler_queue:
    #                           queueName/operation
    patQueue = re.compile("/queue/([a-zA-Z0-9-_]+)/([a-zA-Z]+)/$")
    validClientQOps = [ "consumeMessage", "produceMessage", "stats", "none" ]

    def __init__(self):
        self.queueHandlers, dbList = vars.handler_ah.getQueuesAndDataStores()
        self.queueHandlerPool = { }

        for dbName in dbList:
            self.queueHandlerPool[dbName] = queue_handler(dbName)

        for queue in self.queueHandlers:
            dbName     = self.queueHandlers[queue][0]
            queueState = self.queueHandlers[queue][1]
            self.queueHandlers[queue] = self.queueHandlerPool[dbName]
            self.queueHandlers[queue].set_state(queue, queueState)


    def handleRequest(self, request):
        m = handler_queue.patQueue.match(request.resource)
        if not m:
            # Send back a resource not found error
            request.error_enqueue(404)
            return

        queueName = m.group(1)
        queueOperation = m.group(2)
        if queueName not in self.queueHandlers or queueOperation not in handler_queue.validClientQOps:
            request.error_enqueue(404)
            return

        # The queue could have been deleted by now
        thisQueueHandler = self.queueHandlers[queueName]
        if not thisQueueHandler:
            request.error_enqueue(404)
            return

        # So far so good. Forward the request to the appropriate queue processor
        request.pymq_type    = m.group(2)
        request.pymq_queue   = m.group(1)
        if not hasattr(request, "pymq_payload"):
            request.pymq_payload = ""
        thisQueueHandler.handleRequest(request)


    def informQueueHandler(self, request):
        # TODO: Code here to lock a mutex and update the local hash table
        # for operations that require it (if needed)
        realQueueHandler = None

        if request.pymq_type in [ "addDB" ]:
            dbName = request.pymq_db
            ioUnitHandler = queue_handler(dbName)
            if ioUnitHandler:
                self.queueHandlerPool[dbName] = ioUnitHandler
            request.done_enqueue()
            return

        elif request.pymq_type == "addQueue":
            dbName = str(request.pymq_db)
            self.queueHandlers[request.pymq_queue] = self.queueHandlerPool[dbName]
            realQueueHandler = self.queueHandlers[request.pymq_queue]

        elif request.pymq_type == "dropQueue":
            realQueueHandler = self.queueHandlers[request.pymq_queue]
            self.queueHandlers[request.pymq_queue] = None

        else:
            realQueueHandler = self.queueHandlers[request.pymq_queue]

        realQueueHandler.handleRequest(request)
